package xyz.hujf.project.collect.CHM;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Hujf
 * @title: TestMap
 * @date 2021-05-03 19:13
 * @description: TODO
 */
public class TestMap {
    static ConcurrentHashMap<String, List<String>> map = new ConcurrentHashMap();

    public static void main(String[] args) {
        Thread t1  = new Thread(new Runnable() {
            @Override
            public void run() {
                List list1 = new ArrayList();
                list1.add("de1");
                list1.add("de2");
                List list = map.putIfAbsent("topic1", list1);
                if(list!=null){
                    list.add(list1);
                }
                System.out.println(JSON.toJSONString(map));
            }
        });
        Thread t2  = new Thread(new Runnable() {
            @Override
            public void run() {
                List list1 = new ArrayList();
                list1.add("de11");
                list1.add("de22");
                List list = map.putIfAbsent("topic1", list1);
                if(list!=null){
                    list.add(list1);
                }
                System.out.println(JSON.toJSONString(map));
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                List list1 = new ArrayList();
                list1.add("de111");
                list1.add("de222");
                List list = map.putIfAbsent("topic2", list1);
                if(list!=null){
                    list.add(list1);
                }
                System.out.println(JSON.toJSONString(map));
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}
